home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 0362 / bcsdk.zip / RUNPROG.C < prev    next >
C/C++ Source or Header  |  1995-04-01  |  5KB  |  214 lines

  1. /*
  2.     BarClock(tm)
  3.  
  4.     Keyword Extension Sample
  5.  
  6.     Copyright (c) 1994  Patrick Breen
  7.     All rights reserved.
  8.  
  9.     Contact Information:
  10.  
  11.         Atomic Dog Software
  12.         PO Box 523
  13.         Medford, MA 02155
  14.  
  15.         Phone (617) 396-2673
  16.         Fax   (617) 396-5761
  17.  
  18.         Internet:            pbreen@world.std.com
  19.         CompuServe:         70312,743
  20.         America Online:     PBreen
  21. */
  22.  
  23. #include "bchook.h"
  24. #include <shellapi.h>
  25. #include <commdlg.h>
  26.  
  27. BOOL CALLBACK RunProgram(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
  28.  
  29. // Handle to the DLL instance from Libinit.asm
  30. HANDLE LIBINST;
  31.  
  32. void cdecl _cexit(void)
  33. {
  34. }
  35.  
  36. // Standard library initialization
  37. int FAR PASCAL LibMain(HINSTANCE hInstance, WORD a, WORD b, LPSTR c)
  38. {
  39.     LIBINST = hInstance;
  40.     return 1;
  41. }
  42.  
  43. // Standard library exit
  44. int FAR PASCAL WEP(int a)
  45. {
  46.     return TRUE;
  47. }
  48.  
  49. // Standard version number return
  50. DWORD FAR _export BCHookVersion(DWORD FAR *pSig)
  51. {
  52.     (*pSig) = 0x01534441L;        // 'ADS' (in reverse) x1
  53.     return HOOKVERSION;
  54. }
  55.  
  56. // Return the number of buttons we support
  57. BYTE FAR _export BCBtnCount(void)
  58. {
  59.     // We support 1 buttons
  60.     return 1;
  61. }
  62.  
  63. // Return the label for the button
  64. void FAR _export BCBtnLabel(BYTE btnId, LPSTR pLabelBuf)
  65. {
  66.     // Copy appropriate label into buffer
  67.     if (btnId == 0) lstrcpy(pLabelBuf, "Run Program");
  68.     return;
  69. }
  70.  
  71. // Handle a button click
  72. void FAR _export BCBtnClick(BYTE btnId, BOOL bLeft)
  73. {
  74.     // Display a dialog
  75.     DialogBox(LIBINST, MAKEINTRESOURCE(400), (HWND) 0, RunProgram);
  76.     return;
  77. }
  78.  
  79. static BOOL NEAR GetFile(HWND hwnd, short id)
  80. {
  81.     char file[256];
  82.     OPENFILENAME ofn;
  83.  
  84.     file[0] = 0;
  85.  
  86.     // Initialize the OFN struct
  87.     ofn.lStructSize = sizeof(OPENFILENAME);
  88.     ofn.hwndOwner = hwnd;
  89.     ofn.lpstrFilter = "Programs\0*.exe *.com *.bat *.pif *.wbt\0";
  90.     ofn.nFilterIndex = 1;
  91.     ofn.lpstrCustomFilter = NULL;
  92.     ofn.nMaxCustFilter = 0;
  93.     ofn.lpstrFileTitle = NULL;
  94.     ofn.nMaxFileTitle = 0;
  95.     ofn.lpstrTitle = NULL;
  96.     ofn.lpstrFile= file;
  97.     ofn.nMaxFile = sizeof(file);
  98.     ofn.lpstrInitialDir = NULL;
  99.     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
  100.     ofn.lpstrDefExt = "exe";
  101.     ofn.lpfnHook = NULL;
  102.     ofn.lpTemplateName = NULL;
  103.  
  104.     // Open the dialog
  105.     if (GetOpenFileName(&ofn)) {
  106.  
  107.         // The user confirmed, set the item text
  108.         SetDlgItemText(hwnd, id, file);
  109.         return TRUE;
  110.     }
  111.  
  112.     return FALSE;
  113. }
  114.  
  115.  
  116. BOOL CALLBACK RunProgram(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  117. {
  118.     char prog[300];
  119.     char dir[256];
  120.     char tag[8];
  121.     char save = 0;
  122.     LPSTR pArg;
  123.     BOOL bMin;
  124.     short i;
  125.  
  126.     // Handle init dialog
  127.     if (msg == WM_INITDIALOG) {
  128.  
  129.         // Refresh combo box
  130.         for (i = 0; i < 20; i++) {
  131.  
  132.             // Get the next command
  133.             wsprintf(tag, "Cmd%d", i);
  134.             if (GetPrivateProfileString("RunProg", tag, "", prog, sizeof(prog), "BARCLOCK.INI") == 0)
  135.                 break;
  136.  
  137.             SendDlgItemMessage(hwndDlg, 100, CB_ADDSTRING, 0, (LPARAM) prog);
  138.         }
  139.  
  140.         // Default to last command
  141.         SendDlgItemMessage(hwndDlg, 100, CB_SETCURSEL, 0, 0);
  142.  
  143.     // If confirmed
  144.     } else if (msg == WM_COMMAND) {
  145.  
  146.         switch (wParam) {
  147.  
  148.             case 103:
  149.                 // Check for browse command
  150.                 GetFile(hwndDlg, 100);
  151.                 return TRUE;
  152.  
  153.             case IDOK:
  154.                 // Get dialog info
  155.                 GetDlgItemText(hwndDlg, 100, prog, sizeof(prog));
  156.                 GetDlgItemText(hwndDlg, 101, dir, sizeof(dir));
  157.                 bMin = IsDlgButtonChecked(hwndDlg, 102);
  158.  
  159.                 // Now, run forward from the start looking
  160.                 // for a space - we want to use ShellExecute
  161.                 // so we need to seperate the .EXE from any
  162.                 // arguments that have been provided
  163.                 pArg = prog;
  164.                 while (*pArg && *pArg != ' ') pArg++;
  165.  
  166.                 // We found a space, terminate
  167.                 // app string and step past space
  168.                 if (*pArg == ' ') {
  169.                     save = *pArg;
  170.                     *pArg++ = 0;
  171.                 }
  172.  
  173.                 // Run the application
  174.                 if (ShellExecute(0, NULL, prog, pArg, dir, (bMin)? SW_SHOWMINIMIZED:SW_SHOWNORMAL) <= (HINSTANCE) 32) {
  175.                     MessageBox(hwndDlg, "Could not run program.", "Error!", MB_OK);
  176.                     return TRUE;
  177.                 }
  178.  
  179.                 // Restore blasted char
  180.                 if (save) pArg[-1] = save;
  181.  
  182.                 // If command is not identical - reorder history
  183.                 if ((GetPrivateProfileString("RunProg", "Cmd0", "", dir, sizeof(dir), "BARCLOCK.INI") == 0) ||
  184.                     (lstrcmpi(prog, dir) != 0)) {
  185.  
  186.                     // Update command history
  187.                     for (i = 19; i >= 0; i--) {
  188.  
  189.                         // If command exists
  190.                         wsprintf(tag, "Cmd%d", i);
  191.                         if (GetPrivateProfileString("RunProg", tag, "", dir, sizeof(dir), "BARCLOCK.INI") > 0) {
  192.  
  193.                             // Move to the next slot
  194.                             wsprintf(tag, "Cmd%d", i + 1);
  195.                             WritePrivateProfileString("RunProg", tag, dir, "BARCLOCK.INI");
  196.                         }
  197.                     }
  198.  
  199.                     // Put the latest in the first slot
  200.                     WritePrivateProfileString("RunProg", "Cmd0", prog, "BARCLOCK.INI");
  201.                 }
  202.  
  203.             case IDCANCEL:
  204.                 // Any command message causes this
  205.                 // dialog to go awaw
  206.                 EndDialog(hwndDlg, wParam);
  207.                 return TRUE;
  208.         }
  209.     }
  210.  
  211.     return (msg == WM_INITDIALOG);
  212. }
  213.  
  214.